package com.readboottest.util;


import com.readboottest.entity.IpAgent;
import com.readboottest.ipagent.IpAgentService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * @author zhanghuiyong
 * @创建时间 2019年03月04日
 * @描述: httpClient工具类
 **/

public class HttpClientUtil {
    public static void main(String[] args) {
        String httpUrl = "https://m.miaojiang8.com/0_12/729318.html";
        String doGet = doGet(httpUrl, "gbk");
        System.out.println(doGet);
    }

    /**
     * get请求
     *
     * @param httpHrl 网址
     */
    public static String doGet(String httpHrl, String codeType) {
        //返回结果
        String content = null;
        // 创建httpClient实例
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httpGet实例
        HttpGet httpget = new HttpGet(httpHrl);
        // 模拟浏览器 ✔
        httpget.setHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");

        // 使用代理 IP ✔
        /**
         * 代理IP查询网站
         * @see http://www.data5u.com
         * */
        HttpHost proxy = getProxy();
        RequestConfig config = RequestConfig.custom().setProxy(proxy)
                //设置连接超时 ✔
                // 设置连接超时时间 10秒钟
                .setConnectTimeout(10000)
                // 设置读取超时时间10秒钟
                .setSocketTimeout(10000)
                .build();
        httpget.setConfig(config);
        CloseableHttpResponse response = null;
        try {
            // 执行get请求
            response = httpclient.execute(httpget);
            HttpEntity entity = (HttpEntity) response.getEntity();
            // 获取返回实体
            content = EntityUtils.toString((org.apache.http.HttpEntity) entity, codeType);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close(); // response关闭
                httpclient.close(); // httpClient关闭
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }

    private static HttpHost getProxy() {
        List<IpAgent> agentIps = IpAgentService.getAgentIps();

        IpAgent ipAgent = agentIps.get(new Random().nextInt(agentIps.size()));
//        while ("http".equals(ipAgent.getType())) {
//            ipAgent = agentIps.get(new Random().nextInt(agentIps.size()));
//            System.out.println(ipAgent.getType());
//        }
        HttpHost proxy = new HttpHost(ipAgent.getIp(), ipAgent.getPort());
//        HttpHost proxy = new HttpHost("182.18.13.149", 53281);
        return proxy;
    }
}
